package com.sanji.register.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sanji.register.model.User;
import com.sanji.register.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * Created by baozhen on 17-5-18.
 */
@Component
public class Receiver {

    @Autowired
    private UserRepository userRepository;


    @KafkaListener(topics = {"my-topic","my-topic2"},group = "myGroup")
    public void processMessage(String content) throws  Exception{




           JSONObject j = JSON.parseObject(content);

           String id = j.getString("id");
           String name = j.getString("name");
           String password = j.getString("password");
           String phone = j.getString("phone");

           if(!StringUtils.isEmpty(id)){
               User  user =    userRepository.findOne(id);
               if(!StringUtils.isEmpty(name)){

                   user.setName(name);
               }

               if(!StringUtils.isEmpty(password)){
                   user.setPassword(password);
               }

               if(!StringUtils.isEmpty(phone)){
                   user.setPhone(phone);
               }
               userRepository.save(user);
           }

    }
}
